package dao;

import entity.Product;
import entity.User;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class ProductDao {
    private static ResultSet result = null;
    private static PreparedStatement statement = null;

    /**
     *根据商品的ID查改该商品
     * @param productId 商品的ID
     * @return
     * @throws SQLException
     */
    public static Product getProduct(String productId) throws SQLException {
        Product product = null;
        String sql = "SELECT * FROM product WHERE product_id = " + productId;
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        result = statement.executeQuery();
        while(result.next()){
            product = new Product(result.getInt("product_id"),result.getString("product_name"),result.getInt("category_id"),
                    result.getDouble("price"),result.getString("note"),result.getString("default_img"),
                    result.getInt("hot_id"),result.getInt("review_id"),result.getInt("store"));
        }
        DataBaseOperation.close(result,statement);
        return product;
    }

    /**
     * 添加购物车
     * @param userId 用户ID
     * @param productId 商品ID
     * @return
     * @throws SQLException
     */
    public static String addCart(String userId,String productId,String number) throws SQLException {
        int store = productExitsStore(productId);
        if(store <= 0) return "Not Store";
        if(repairAddCart(productId,userId,Integer.parseInt(number)).equals("Success")) return "Success";

        String sql = "INSERT INTO cart(user_id,product_id,payment_number) " +
                "VALUES(" + Integer.parseInt(userId) + "," + Integer.parseInt(productId) + "," + Integer.parseInt(number) + ")";

        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        int result = statement.executeUpdate();
        DataBaseOperation.close(statement);
        return result > 0 ? "Success" : "Error";
    }

    /**
     * 查看商品是否还有库存
     * @param productId 商品ID
     * @return
     */
    public static int productExitsStore(String productId){
        String sql = "SELECT store FROM product WHERE product_id = " + productId;
        int store = 0;
        try {
            statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
            result = statement.executeQuery();
            while(result.next()){
                store = result.getInt("store");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DataBaseOperation.close(result,statement);
        return store;
    }

    /**
     * 得到指定商品的评论
     * @param productId 商品ID
     * @return
     * @throws SQLException
     */
    public static Map<String, User> getProductReview(String productId) throws SQLException {
        Map<String,User> reviewList = new HashMap<>();
        String sql = "SELECT * FROM review WHERE product_id = " + productId;
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        result = statement.executeQuery();
        while(result.next()){
            reviewList.put(result.getString("content"),UserDao.getUser(result.getString("user_id")));
        }
        DataBaseOperation.close(result,statement);
        return reviewList;
    }

    //判断在购物车中是否存在当前加入购物车的商品，有则在基本购物数量上加上当前的购买数量
    public static String repairAddCart(String productId,String userId,int number) throws SQLException {
        String sql = "SELECT * FROM cart WHERE product_id = " + productId;
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        result = statement.executeQuery();
        int paymentNumber = 0;
        while(result.next()){
            paymentNumber = result.getInt("payment_number");
        }
        if(paymentNumber == 0) return "Error";
        sql = "UPDATE cart SET payment_number = " + (paymentNumber + number) + " WHERE product_id = " + productId + " AND user_id = " + userId;
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        return statement.executeUpdate() > 0 ? "Success" : "Error";
    }

}
